﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using FirstFloor.ModernUI.Windows.Controls;
using YanMsg.APCode;

namespace YanMsg.View
{
    /// <summary>
    /// ChatHistory.xaml 的交互逻辑
    /// </summary>
    public partial class GroupChatHistory : ModernWindow
    {

        static int ThisGroupID = 0;


        public GroupChatHistory()
        {
            this.InitializeComponent();
            // 在此点之下插入创建对象所需的代码。
        }
        public GroupChatHistory(int GroupID)
        {
            this.InitializeComponent();
            this.Style = (Style)App.Current.Resources["EmptyWindow"];
            ThisGroupID = GroupID;
            this.RTBChatHistory.AppendText("正在加载历史记录，请稍后...");
            Task getMsgTask = new Task(() =>
            {
                this.Dispatcher.Invoke(new Action(() =>
                {
                    BindChatPeople(GroupID);
                    GetHistoryMsg(GroupID);
                }));
            });
            getMsgTask.Start();
        }





        //绑定聊天人
        public void BindChatPeople(int GroupID)
        {
            try
            {
                DataTable dtUser = new DataTable();
                dtUser = ChatHelp.GetGroupChatUses(GroupID);
                Dictionary<string, string> dic = new Dictionary<string, string>();
                for (int i = 0; i < dtUser.Rows.Count; i++)
                {
                    dic.Add(dtUser.Rows[i]["CRUser"].ToString(), UserData.ListPeople.Where(d => d.UserName == dtUser.Rows[i]["CRUser"].ToString()).SingleOrDefault().UserRealName);
                }
                dic.Add("", "全部");

                CBXPeople.ItemsSource = dic;
                CBXPeople.SelectedValuePath = "Key";
                CBXPeople.DisplayMemberPath = "Value";
                CBXPeople.SelectedValue = "";
            }
            catch (Exception error)
            {
                MsgManger.Show(error.Message.ToString());
            }
        }



        private void CBXPeople_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.RTBChatHistory.Document.Blocks.Clear();
            this.RTBChatHistory.AppendText("正在加载历史记录，请稍后...");
            KeyValuePair<string, string> Item = (KeyValuePair<string, string>)CBXPeople.SelectedItem;
            Task getMsgTask = new Task(() =>
            {
                this.Dispatcher.Invoke(new Action(() =>
                {
                    GetHistoryMsg(ThisGroupID, Item.Key.ToString());
                }));
            });
            getMsgTask.Start();
        }




        //获取记录
        private void GetHistoryMsg(int GroupID, string strUserName = "")
        {
            try
            {
                this.RTBChatHistory.Document.Blocks.Clear();
                DataTable ListMsg = ChatHelp.GetGroupChatHis(GroupID, strUserName);

                for (int i = 0; i < ListMsg.Rows.Count; i++)
                {
                    string msgConTent = ZipHelper.GZipDecompressString(ListMsg.Rows[i]["GMsgContent"].ToString());

                    //将信息加载到临时RichTextBox
                    System.Windows.Controls.RichTextBox RTemp = new System.Windows.Controls.RichTextBox();
                    ChatManger.LoadFromRTF(RTemp, msgConTent);

                    //添加信息头
                    string strMSTitle = ChatManger.GetUserName(ListMsg.Rows[i]["CRUser"].ToString()) + "  " + DateTime.Parse(ListMsg.Rows[i]["CRDate"].ToString()).ToString("yyyy-MM-dd HH:mm");
                    TextBlock TB = new TextBlock(new Bold(new Run(strMSTitle)));
                    if (ListMsg.Rows[i]["CRUser"].ToString() == UserData.UserNumber)
                    {
                        TB.Foreground = new SolidColorBrush(Colors.Blue);
                    }
                    BlockUIContainer BU = new BlockUIContainer(TB);

                    this.RTBChatHistory.Document.Blocks.Add(BU);
                    //添加信息内容
                    for (int m = RTemp.Document.Blocks.Count - 1; m >= 0; m--)
                    {
                        var q = RTemp.Document.Blocks.ElementAt(m);
                        this.RTBChatHistory.Document.Blocks.Add(q);
                    }
                }

            }
            catch (Exception error)
            {
                MsgManger.Show("无法得到查询结果");
            }
        }

        private void CloseButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}